//
//  SelecctAddress.m
//  ios-supplier
//
//  Created by Benlai on 15/8/21.
//  Copyright (c) 2015年 feynmanyuan. All rights reserved.
//

#import "SelecctAddressDS.h"

@implementation SelecctAddressDS
- (instancetype)initWithAttributes:(NSDictionary *)attributes {
    self = [super init];
    if (!self) {
        return nil;
    }

    self.ID = [[attributes objectForKey: @"id"] isEqual: [NSNull null]] ? 0 : [[attributes objectForKey: @"id"]  integerValue];
    self.address_id = [[attributes objectForKey: @"address_id"] isEqual: [NSNull null]] ? 0 : [[attributes objectForKey: @"address_id"]  integerValue];
    self.is_default = [[attributes objectForKey: @"is_default"] isEqual: [NSNull null]] ? 0 : [[attributes objectForKey: @"is_default"]  integerValue];
    self.shop_id = [[attributes objectForKey: @"shop_id"] isEqual: [NSNull null]] ? 0 : [[attributes objectForKey: @"shop_id"]  integerValue];
    self.address_name = [[attributes objectForKey: @"address_name"] isEqual: [NSNull null]] ? 0 : [attributes objectForKey: @"address_name"];
    self.create_at = [[attributes objectForKey: @"create_at"] isEqual: [NSNull null]] ? 0 : [attributes objectForKey: @"create_at"];
    self.label_name = [[attributes objectForKey: @"label_name"] isEqual: [NSNull null]] ? 0 : [attributes objectForKey: @"label_name"];
    self.receiver = [[attributes objectForKey: @"receiver"] isEqual: [NSNull null]] ? 0 : [attributes objectForKey: @"receiver"];
    self.receiver_phone = [[attributes objectForKey: @"receiver_phone"] isEqual: [NSNull null]] ? 0 : [attributes objectForKey: @"receiver_phone"];
    self.update_at = [[attributes objectForKey: @"update_at"] isEqual: [NSNull null]] ? 0 : [attributes objectForKey: @"update_at"];
    self.address_detail = [[attributes objectForKey: @"address_detail"] isEqual: [NSNull null]] ? 0 : [attributes objectForKey: @"address_detail"];
    self.city_id = [[attributes objectForKey: @"city_id"] isEqual: [NSNull null]] ? 0 : [[attributes objectForKey: @"city_id"]  integerValue];
    self.city_name = [attributes objectForKey: @"city_name"];
    self.region_name = [attributes objectForKey: @"region_name"];

    return self;
}

+ (NSURLSessionDataTask *)loadAddressList:(void (^)(NSArray *addressList, NSError *error))block
{
    return [[JFBAPIClient sharedClient: YES] GET: @"/shop/purchase/address.json" parameters: nil success:^(NSURLSessionDataTask * __unused task, id JSON) {
        NSObject * error = [JSON valueForKey:@"error"];
        NSLog(@"%@", JSON);
        if( error == [NSNull null] || [error.description isEqualToString:@""]) {
            NSMutableArray *result = [@[] mutableCopy];
            NSArray *addressArray = [JSON valueForKeyPath:@"data"];
            for( NSDictionary *staff in addressArray ) {
                SelecctAddressDS * address = [[SelecctAddressDS alloc] initWithAttributes:staff];
                [result addObject: address];
            }
            block([NSArray arrayWithArray:result], nil);
        } else {
            block(nil, nil);
        }
    } failure:^(NSURLSessionDataTask *__unused task, NSError *error) {
        block(nil, error);
    }];
}

+ (NSURLSessionDataTask *)addReceiveAddress:(void (^)(BOOL isSuccess, NSString *message, NSError *error))block receiveAddress:(SelecctAddressDS*)receiveAddress
{
    NSDictionary *addressParams = @{@"receiver": receiveAddress.receiver,
                                    @"address_name":receiveAddress.address_name,
                                    @"is_default":receiveAddress.is_default == 0 ? @(NO) : @(YES),
                                    @"receiver_phone":receiveAddress.receiver_phone,
                                    @"address_detail": receiveAddress.address_detail,
                                    @"address_type": [NSNumber numberWithInt:2],
                                    @"address_id": [NSNumber numberWithInt: -1],
                                    @"region_id": [NSNumber numberWithInteger:receiveAddress.regionID]};
    
    return [[JFBAPIClient sharedClient:true] GET:@"/shop/purchase/address/add.json" parameters:addressParams success:^(NSURLSessionDataTask * __unused task, id JSON) {
        NSObject * error = [JSON valueForKey:@"error"];
        if( error == [NSNull null] ) {
            block(true, @"添加成功", nil);
        } else {
            block(false, @"请填写配送员信息", nil);
        }
    } failure:^(NSURLSessionDataTask *__unused task, NSError *error) {
        block(false, @"添加失败", error);
    }];
    return nil;
}

+ (NSURLSessionDataTask *)updateReceiveAddress:(void (^)(BOOL isSuccess, NSString *message, NSError *error))block receiveAddress:(SelecctAddressDS*)receiveAddress
{
    NSDictionary *addressParams = @{@"receiver": receiveAddress.receiver,
                                    @"address_name":receiveAddress.address_name,
                                    @"is_default":receiveAddress.is_default == 0 ? @(NO) : @(YES),
                                    @"receiver_phone":receiveAddress.receiver_phone,
                                    @"address_detail": receiveAddress.address_detail,
                                    @"address_type": [NSNumber numberWithInt:2],
                                    @"address_id": [NSNumber numberWithInt: -1],
                                    @"region_id": [NSNumber numberWithInteger:receiveAddress.regionID],
                                    @"shop_address_id": [NSNumber numberWithInteger:receiveAddress.ID]};
    
    return [[JFBAPIClient sharedClient:true] GET:@"/shop/purchase/address/update.json" parameters:addressParams success:^(NSURLSessionDataTask * __unused task, id JSON) {
        NSObject * error = [JSON valueForKey:@"error"];
        if( error == [NSNull null] ) {
            block(true, @"添加成功", nil);
        } else {
            block(false, @"请填写配送员信息", nil);
        }
    } failure:^(NSURLSessionDataTask *__unused task, NSError *error) {
        block(false, @"添加失败", error);
    }];
    return nil;
}
@end
